package com.googlecode.jmicroerp.service;

import com.googlecode.jmicroerp.repository.ApometreRepository;
import java.math.BigDecimal;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 * @author Alexandru.Codescu
 */
@Service(value = "apometreService")
public class ApometreService {
    @Autowired
    private ApometreRepository apometreRepository;
    
    private GregorianCalendar getTodayCalendar() {
        GregorianCalendar calendar = new GregorianCalendar();
        calendar.set(GregorianCalendar.HOUR_OF_DAY, 0);
        calendar.set(GregorianCalendar.MINUTE, 0);
        calendar.set(GregorianCalendar.SECOND, 0);
        //calendar.set(GregorianCalendar.AM_PM, GregorianCalendar.AM);
        calendar.set(GregorianCalendar.MILLISECOND, 0);
        return calendar;
    }
    
    private Date getTodayDate() {
        return getTodayCalendar().getTime();
    }
    
    private Date get7DayDate() {
        GregorianCalendar calendar = getTodayCalendar();
        calendar.add(GregorianCalendar.DATE, -7);
        
        return calendar.getTime();
    }
    
    private Date getLastMonthDate() {
        GregorianCalendar calendar = getTodayCalendar();
        calendar.set(GregorianCalendar.DATE, 1);
        
        return calendar.getTime();
    }
    
/*     public List<Object[]> queryByRecipient() {
        Date today = getTodayDate();
        return apometreRepository.queryByRecipientAll(1L);
        
    } */
    
    public Object[] queryByRecipientAll(Long idRecipient, Date dataC) {
        // Date today = getTodayDate();
        List<Object[]> objs = apometreRepository.queryByRecipientAll(idRecipient, dataC);
        if (objs.isEmpty()) {
                return null;
            } else {
                return objs.get(0);
            }
    }
    
    public BigDecimal lastIndexByRecipient(Long idRecipient) {
        List<BigDecimal> valori = apometreRepository.lastIndexByRecipient(idRecipient);
        if (valori.isEmpty()) {
            return BigDecimal.ZERO;
        } else {
            return valori.get(0);
        }
    }
    
    public List<Object[]> queryApometre(Long idRecipient, Date dataS) {
        // Date today = getTodayDate();
        return apometreRepository.queryApometre(idRecipient);
        
    }

}